package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper,Appointment> implements IAppointmentService {

    @Autowired
    private cn.wolfcode.car.business.mapper.AppointmentMapper appointmentMapper;

    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        IPage<Appointment> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<Appointment> wrapper = Wrappers.<Appointment>query();
        wrapper.like(StringUtils.hasText(qo.getCustomerName()),"customer_name",qo.getCustomerName());
        wrapper.like(StringUtils.hasText(qo.getCustomerPhone()),"customer_phone",qo.getCustomerPhone());
        wrapper.eq(qo.getStatus()!=null,"status",qo.getStatus());
        wrapper.eq("soft_delete",0);
        IPage<Appointment> appointmentIPage = appointmentMapper.selectPage(page, wrapper);
        return new TablePageInfo<Appointment>(super.page(appointmentIPage,wrapper));
    }


    @Override
    public void insert(Appointment appointment) {
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        //只有预约中才能修改
        Appointment old = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(old.getStatus())) {
            throw new BusinessException("此项目处于预约中不能修改");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids, Integer statusDelete) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //只有预约中才能修改
            Appointment old = this.get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(old.getStatus())) {
                throw new BusinessException("此项目处于预约中不能修改");
            }
            appointmentMapper.deleteByPrimaryKey(dictId,statusDelete);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    @Override
    public void cancel(Long id, Integer status) {
        //只有预约中才能取消
        Appointment old = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(old.getStatus())) {
            throw new BusinessException("此项目处于预约中不能修改");
        }
        appointmentMapper.cancel(id, status);
    }

    @Override
    public void arrival(Long id, Integer status) {
        //只有预约中才能改为进店
        Appointment old = this.get(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(old.getStatus())) {
            throw new BusinessException("此项目处于预约中不能修改");
        }
        appointmentMapper.arrival(id, status,new Date());
    }

    @Override
    public void changeStatus(Long appointmentId, Integer status) {
        appointmentMapper.changeStatus(appointmentId,status);
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        //合理性判断
        //处于已到店 | 结算单生成  逻辑才成立
        Appointment appointment = this.get(appointmentId);
        if (!(Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())||
            Appointment.STATUS_SETTLE.equals(appointment.getStatus()))
        ){
            throw new BusinessException("处于已到店 | 结算单生成才能点击结算单按钮");
        }
        Statement statement = statementService.selectByAppointmentId(appointmentId);
        //如果statement为空,说明预约单没有生成过结算单.
        if (statement==null){
            //把预约单的信息封装到结算单里
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());//客户姓名
            statement.setCustomerPhone(appointment.getCustomerPhone());//客户联系方式
            statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
            statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
            statement.setCarSeries(appointment.getCarSeries());//汽车类型
            statement.setServiceType(appointment.getServiceType());//服务类型
            statement.setAppointmentId(appointment.getId());
            statement.setCreateTime(appointment.getCreateTime());
            statement.setInfo(appointment.getInfo());
            //把预约单信息封装成结算单对象,保存结算单对象
            statementService.save(statement);
            return statement;
        }
        return statement;
    }
}
